//
// FixedPointMethod.cpp
// Fixed point method implementation.
//
// Author: Gabriel H. Barbuto (gbarbuto@dc.uba.ar)
//
#include <cmath>
#include <cstddef>
#include <functional>
#include <vector>

#include "FixedPointMethod.h"

FixedPointMethod::FixedPointMethod(std::function<double(double)> const &g, std::size_t const &iterations) :
UnaryIterativeMethod(iterations), _g(g)
{
}

FixedPointMethod::FixedPointMethod(std::function<double(double)> const &g, double const &epsilon, std::size_t const &iterations) :
UnaryIterativeMethod(epsilon, iterations), _g(g)
{
}

double FixedPointMethod::operator()(std::vector<double> const &arguments)
{
	this->check(arguments);

	double x_1(0.0);
	double x(arguments[0]);

	for (std::size_t iteration(0); iteration < this->max_iterations(); ++iteration)
	{
		x_1 = x;
		x = this->_g(x_1);

		if (std::fabs(x - x_1) < this->epsilon())
		{
			this->iterations(iteration);

			return x;
		}
	}

	this->iterations(this->max_iterations());

	return x;
}
